const Base = require('../../base.js');

module.exports = class extends Base {
  /// 用户充值
  async indexAction() {
    let data = this.post()
    let userInfo = await this.session('user_info')
    if (!userInfo) return this.fail(-1, '未登录授权')
    let alipaySdk = await think.alipaySdk()
    if (data.money <= 0) return this.fail(-1, '充值金额错误')
    /// 生成交易订单
    let deal = await this.model('deal/index').add({
      type: 'charge',
      uid: userInfo._id,
      money: data.money,
      status: 0,
      remark: '发起支付宝付款'
    })
// http://yangheng.w3.luyouxia.net/
    let id = deal._id.toString()
    let result = await this.model('alipay/index').wapPay(alipaySdk, {
      notifyUrl: 'http://' + this.ctx.host + '/api/deal/charge?id=' + deal._id,
      // notifyUrl: 'http://yangheng.w3.luyouxia.net/api/deal/charge?id=' + id,
      outTradeNo: id,
      totalAmount: data.money,
      subject: '账户充值',
    })
    // // 设置订单支付方式
    // await this.mongoose('mongo/Pay_order').edit(order.order_id, {
    //   pay_type: 'alipay', // 设置支付方式
    //   order_status: 1 // 设置订单状态
    // })
    this.json({
      status: 200, message: 'success', data: {
        deal, result
      }
    })
    // return this.display();
  }
  async testAction() {
    // let data = this.post()
    // let userInfo = await this.session('user_info')
    // if (!userInfo) return this.fail(-1, '未登录授权')
    let alipaySdk = await think.alipaySdk()

    // /// 生成交易订单
    // let deal = await this.model('deal/index').add({
    //   type: 'charge',
    //   uid: userInfo._id,
    //   money: data.money,
    //   status: 0,
    //   remark: '发起支付宝付款'
    // })

    let id = Date.now()
    let result = await this.model('alipay/index').wapPay(alipaySdk, {
      notifyUrl: 'http://' + this.ctx.host + '/api/deal/charge?id=' + id,
      outTradeNo: id,
      totalAmount: 120,
      subject: '支付订单',
      body: '用户发起支付'
    })
    // // 设置订单支付方式
    // await this.mongoose('mongo/Pay_order').edit(order.order_id, {
    //   pay_type: 'alipay', // 设置支付方式
    //   order_status: 1 // 设置订单状态
    // })
    this.json({
      status: 200, message: 'success', data: {
        deal, result
      }
    })
    // console.log(result)
    // return this.redirect(result)
  }
};
